package gaml.yaml.ahchor;

import gaml.yaml.YamlNode;


/**
 * for subobjects referenced more then one in
 * serializing object use yaml anchor
 * ciclic references resolition
 * @author shnireck
 *
 */
public class YamlAnchor {
	protected Object object = null; 
	protected String code = ""; 
	protected YamlNode node = null;
	
	protected boolean used = false;
	
	/**
	 * yaml anchor for every node
	 * reffers to all node aliases  
	 * @param code anchor unique code
	 * @param node yaml node wich anchor referes to
	 */
	public YamlAnchor (String code, YamlNode node) {
		this.code = code;
		this.node = node;
		
		this.used = false;
	}
	
	/**
	 * yaml anchor for every node
	 * reffers to all node aliases 
	 * @param code unique code for yaml anchor
	 * @param object object, represented by node
	 * @param node node itself
	 */
	public YamlAnchor (String code, Object object, YamlNode node) {
		this.code = code;
		this.object = object;
		this.node = node;
		
		this.used = false;
	}
	
	/**
	 * returns text code for anchor
	 * @return
	 */
	public String getCode () { return code; }
	
	/**
	 * returns java object for anchor
	 * @return java object
	 */
	public Object getObject () {return object; }
	
	/**
	 * sets java object for anchor 
	 * @param object java object for anchor
	 * @return host anchor object
	 */
	public YamlAnchor setObject (Object object) { this.object = object; return this; }
	
	/**
	 * returns yaml node, representing object, this anchor references to
	 * @return
	 */
	public YamlNode getNode () { return node; }
	
	/**
	 * define node, referenced by this anchor
	 * @param node yaml node, wich this anchor references
	 * @return host anchor object
	 */
	public YamlAnchor setNode (YamlNode node) { this.node = node; return this; }
	
	/** check if any aliases where created for anchored node
	 * @return true if aliases where created for node anchored with this anchor, false if anchor not used
	 */
	public boolean isUsed () { return used; }
	
	/** mark this anchor as used, meaning aliases where created for anchored node
	 * @return host anchor object
	 */
	public YamlAnchor setUsed () { used = true; return this;}
}
